package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.Advertisement;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class UserAdvertisementAdapter extends RecyclerView.Adapter<UserAdvertisementAdapter.AdvertisementViewHolder> {

    private Context context;
    private List<Advertisement> advertisements;
    private OnAdvertisementClickListener listener;
    private SimpleDateFormat dateFormat;

    public interface OnAdvertisementClickListener {
        void onAdvertisementClick(Advertisement advertisement);
    }

    public UserAdvertisementAdapter(Context context, OnAdvertisementClickListener listener) {
        this.context = context;
        this.listener = listener;
        this.advertisements = new ArrayList<>();
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    }

    @NonNull
    @Override
    public AdvertisementViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_advertisement_user, parent, false);
        return new AdvertisementViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdvertisementViewHolder holder, int position) {
        Advertisement advertisement = advertisements.get(position);
        
        // 设置广告标题
        holder.tvTitle.setText(advertisement.getTitle());
        
        // 设置广告描述
        if (advertisement.getDescription() != null && !advertisement.getDescription().isEmpty()) {
            holder.tvDescription.setText(advertisement.getDescription());
        } else {
            // 如果没有描述，使用内容的前50个字符
            String content = advertisement.getContent();
            if (content != null && !content.isEmpty()) {
                if (content.length() > 50) {
                    content = content.substring(0, 50) + "...";
                }
                holder.tvDescription.setText(content);
            } else {
                holder.tvDescription.setText("暂无描述");
            }
        }
        
        // 设置发布日期
        String date = advertisement.getCreatedAt() != null ? 
                dateFormat.format(advertisement.getCreatedAt()) : dateFormat.format(new Date());
        holder.tvDate.setText(date);
        
        // 设置广告图片
        // 在实际应用中，应该使用图片加载库如Glide或Picasso加载图片URL
        // 这里为了简化，我们使用默认图像
        holder.ivImage.setImageResource(R.drawable.default_avatar);
        
        // 设置卡片点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onAdvertisementClick(advertisement);
            }
        });
        
        // 设置查看详情按钮点击事件
        holder.btnViewDetails.setOnClickListener(v -> {
            if (listener != null) {
                listener.onAdvertisementClick(advertisement);
            }
        });
    }

    @Override
    public int getItemCount() {
        return advertisements.size();
    }

    public void setData(List<Advertisement> advertisements) {
        this.advertisements = advertisements;
        notifyDataSetChanged();
    }

    public void addData(List<Advertisement> advertisements) {
        this.advertisements.addAll(advertisements);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.advertisements.clear();
        notifyDataSetChanged();
    }

    static class AdvertisementViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImage;
        TextView tvTitle;
        TextView tvDescription;
        TextView tvDate;
        Button btnViewDetails;

        public AdvertisementViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_ad_image);
            tvTitle = itemView.findViewById(R.id.tv_ad_title);
            tvDescription = itemView.findViewById(R.id.tv_ad_description);
            tvDate = itemView.findViewById(R.id.tv_ad_date);
            btnViewDetails = itemView.findViewById(R.id.btn_view_details);
        }
    }
} 